<?php 

/**
* 验证码类
*/
class Code {
	public $configs = array(
		'lenght'=>4,
		'width'=>200,
		'height'=>50,
		'line'=>0,
		'str'=>"qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM",
		'point'=>0,
		'fontsize'=>30
		);
	public $src;
	/**构造函数初始化**/
	function __construct($configs=array())
	{
		$this->configs = array_merge($this->configs,$configs);
	}
	public function __get($key)
	{
		return $this->configs[$key];
	}
	/**
	 * 生成验证码
	 */
	public function show($is_show=true)
	{
		if($is_show) header("Content-type: image/png");

		$this->_createImg();
		$this->line();
		$this->point();
		$this->addFont();
		$this->outPut();
	}

	public function _createImg()
	{
		$this->src = imageCreateTrueColor($this->width,$this->height); 

		//根据画布选择颜色
		$color = imageColorAllocate($this->src,0,255,255); 
		// 填充
		imageFill($this->src,0,0,$color);
		
	}

	public function line()
	{
		for ($i=0; $i <$this->line ; $i++) { 
			$color = imageColorAllocate($this->src,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200));

			imageline ($this->src, mt_rand(0,$this->width) , mt_rand(0,$this->height/2) , mt_rand(0,$this->width) , mt_rand($this->height/2,$this->height), $color);
		}
	}

	public function point()
	{
		for ($i=0; $i < $this->point; $i++) { 
			$color = imageColorAllocate($this->src,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200));
			imagesetpixel ( $this->src , mt_rand(0,$this->width) , mt_rand(0,$this->height), $color);
		}
	}
	public function addFont()
	{
		for ($i=0; $i < $this->lenght; $i++) {

			$code = $this->str[mt_rand(0,strlen($this->str)-1)];

			$width = $i*($this->width/$this->lenght);

			$width+=10;

			$height = ($this->height/2)+($this->fontsize/2);

			$color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,250));
			imagettftext ($this->src,$this->fontsize, mt_rand(-15,15) ,$width ,$height,$color, './FireBlock.ttf' , $code);
		}

	}
	public function outPut()
	{
		imagepng($this->src); 
	}
}











 ?>